<?php
require_once 'CMS/Util.php';
require_once 'CMS/User.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseAdminController.php';
require_once 'CMS/AdminCommon.php';

//Người tạo : Lại Ngọc Thái Châu - Team 0
//Module 	: PageManager
//Chức năng	: Quản lý các content của các page

class Admin_EditPageController extends BaseAdminController {
		
	public function init(){
		parent::init();
		$this->_helper->layout()->setLayout('layoutadmin');
		
	}
	
	public function indexAction(){
		$user = new User();
		$user->init();
		$user->check_login();
		$hasmodify = $user->modifiy("admin/page");
		$this->view->modify = $hasmodify;
		if(isset($_GET['page']) && $_GET['page']!=null && $_GET['page']!="")
		{
			$tbCurrentPage = new Admin_Model_CurrentPage();
			$is_page = $tbCurrentPage->selectPageByCode($_GET['page']);
			if(count($is_page)>0)
			{
				$user = new User();
				$user->init();
				$user->check_login();
				
				$count = 0;
				$tbContentModuletopage = new Admin_Model_ContentModuleToPage();
				$tbDefaultModuletopage = new Admin_Model_DefaultModuleToPage();
				$title_lang=unserialize($is_page[0]['title']);
				$is_page[0]['title']=$title_lang['Default'];
				$page = $is_page[0]['page_code'];
				$this->view->page =$is_page[0]['title'];
				$path = APPLICATION_PATH."/widgets";
				$tree = dir($path);
				while($dir = $tree->Read())
				{
					if (is_dir($path."/".$dir)&&($dir!=".")&&($dir!="..")&&($dir!=".svn"))
					{
						$xmlpath = $path."/".$dir."/";
						$xmlconfig = new SimpleXMLElement($xmlpath."config.xml",null,true);
						if($xmlconfig->type=="content")
						{
							$module = $tbContentModuletopage->selectModuleByCode($xmlconfig->name,$page);
							$default_module = $tbDefaultModuletopage->selectModuleByCode($xmlconfig->name,$page);
							if(count($default_module)>0) 
							{
								$check = 1;
							}
							else 
							{
								$check = 0;
							}
							if(count($module)>0) 
							{
								$haved = $module[0]["sort_order"];
							}
							else 
							{
								$haved = -1;
							}
							$widget  = array(
								"module_code" => $xmlconfig->name,
								"module_type" => $xmlconfig->type,
								"module_title" => $xmlconfig->titleDefault,
								"module_sort" => $check==1 ? $default_module[0]["sort_order"] :$haved ,
								"default" => $check
							);
							$widgets[] = $widget;
						}
					}
				}	
				$this->view->installUrl = "/admin/installwidget/editpage";
				$this->view->unInstallUrl = "/admin/uninstallwidget/editpage";
				$this->view->updateDefaultUrl = "/admin/installwidget/updatedefaultmodule";
				$this->view->updateContentUrl = "/admin/installwidget/updatepage";
				$this->view->widgets = $widgets;
			}
		}
	}
}